package com.zry.yard.ui.activity

import android.content.Intent
import android.view.View
import com.zry.yard.R
import com.zry.yard.adapter.StockProductListAdapter
import com.zry.yard.animation.RecyclerViewUtilKt
import com.zry.yard.been.ResultBean
import com.zry.yard.been.StockProduct
import com.zry.yard.databinding.ActivityStockProductListBinding
import com.zry.yard.retrofit.HttpServer
import com.zry.yard.retrofit.ShowUserView
import com.zry.yard.ui.base.BaseActivity
import com.zry.yard.utils.ACacheUtil
import kotlinx.android.synthetic.main.activity_stock_product_list.*
import kotlinx.android.synthetic.main.common_title.*

class StockProductListActivity : BaseActivity(), ShowUserView {

    private var stockProductListBinding: ActivityStockProductListBinding? = null
    private var stockProductList: MutableList<StockProduct>? = null
    private var stockProductListAdapter: StockProductListAdapter? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_stock_product_list
    }

    override fun initData() {
        titleCenterText.text = "产品列表"
        commonLeftLinearLayout.setOnClickListener { finish() }
        stockProductListBinding = viewDataBinding as ActivityStockProductListBinding
        stockProductListBinding?.handlerClick=this
        initRecycler()

        HttpServer(this).productReport(ACacheUtil().getLoginInfo().EntID, 1, 999, "", this)
    }


    private fun initRecycler() {
        stockProductList = mutableListOf()
        recyclerViewUtilKt = RecyclerViewUtilKt(this, stockProductRecycler)
        recyclerViewUtilKt?.initRecyclerView()
        stockProductListAdapter = StockProductListAdapter(this, R.layout.item_stock_product, stockProductList)
        recyclerViewUtilKt?.setAdapter(stockProductListAdapter!!)
        stockProductListAdapter?.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this, StockProductInfoActivity::class.java)
            intent.putExtra("StockProduct", stockProductList!![position])
            startActivity(intent)
        }
    }


    fun stockProductMethod(view: View) {
        when (view.id) {
            R.id.searchStockProductBtn -> {
                HttpServer(this).productReport(ACacheUtil().getLoginInfo().EntID,
                        1, 999, searchStockProductNameET.text.toString().trim(), this)
            }
        }
    }


    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            38 -> {
                val resultData = data as ResultBean<MutableList<StockProduct>>
                if (resultData.State == "1") {
                    stockProductList!!.removeAll(stockProductList!!)
                    stockProductList!!.addAll(resultData.ResultData)
                    stockProductListAdapter?.notifyDataSetChanged()
                } else {
                    toast(resultData.Message)
                }
            }
        }
    }
}
